package cn.edu.njust.service;

import cn.edu.njust.dao.CourseDAOImpl;
import cn.edu.njust.dao.HitCourseDAOImpl;
import cn.edu.njust.entity.Course;
import cn.edu.njust.entity.HitCourse;
import cn.edu.njust.entity.Page;

import java.util.ArrayList;

public class CourseService {
    public ArrayList<Course> selectAll(int i){
        CourseDAOImpl dao = new CourseDAOImpl();
        ArrayList<Course> list;
        list = dao.selectAll(i);
        return list;
    }
    public ArrayList<Course> selectAll(){
        return selectAll(0);
    }

    public int add(Course course){
        CourseDAOImpl dao = new CourseDAOImpl();
        return dao.add(course);
//        return b;
    }

    public int add(String cName, String _cNum, String _cType){
        int cNum = Integer.parseInt(_cNum);
        int cType = Integer.parseInt(_cType);
        Course course = new Course();
        course.setNum(cNum);
        course.setName(cName);
        course.setType(cType);
        return add(course);
    }

    public boolean sub(int id){
        CourseDAOImpl dao = new CourseDAOImpl();
        return dao.sub(id);
    }

    public boolean sub(String tag){
        CourseDAOImpl dao = new CourseDAOImpl();
        String[] checked = tag.split(",");
        boolean[] b = new boolean[checked.length];
        for (int i = 0; i < checked.length; i++){
            b[i] = sub(Integer.parseInt(checked[i]));
            if(!b[i]){
                return false;
            }
        }
        return true;
    }

    public int HitCourse(int uId,int cId){
        HitCourseDAOImpl dao = new HitCourseDAOImpl();
        return dao.HitC(uId,cId);
    }

    public int ExitCourse(int uId,int cId){
        HitCourseDAOImpl dao = new HitCourseDAOImpl();
        return dao.ExitC(uId,cId);
    }

    public ArrayList<HitCourse> selectAllHit(int uId){
        HitCourseDAOImpl dao = new HitCourseDAOImpl();
        return dao.selectAll(uId);
    }

    // 通过页面返回课程集合
    public Page selectByPage(int type,String _currentPage){
        int currentPage;
        if(_currentPage == null || "".equals(_currentPage)){
            currentPage = 1;
        }
        else
            currentPage = Integer.parseInt(_currentPage);
        // 新建page对象
        Page page = new Page();
        // 通过currentPage计算start
        // 每页显示10条数据

//        System.out.println(start);
        CourseDAOImpl dao = new CourseDAOImpl();
        int totalCount = dao.getTotalCount();
        int totalPage;
        if(totalCount % 10 == 0)
            totalPage = totalCount / 10;
        else
            totalPage = totalCount / 10 + 1;
        if (currentPage <= 1) {
            currentPage = 1;
            page.setPrevPage(1);
        }
        else
            page.setPrevPage(currentPage - 1);
        if(currentPage >= totalPage) {
            currentPage = totalPage;
            page.setNextPage(currentPage);
        }
        else
            page.setNextPage(currentPage + 1);

        int start = (currentPage - 1) * 10;
        ArrayList<Course> list = dao.selectByPage(type,start);

        page.setPageNo(currentPage);
        page.setList(list);
        page.setPageSize(10);
        page.setTotalSize(totalCount);
        page.setTotalPage(totalPage);

        return page;
    }

    public Page selectByPage(String _currentPage){
        return selectByPage(0,_currentPage);
    }

    public HitCourse HitCourse(int uId, String cIds){
        HitCourseDAOImpl dao = new HitCourseDAOImpl();
        String[] _cId = cIds.split(",");        //以”,“为分割符，切割传入字符并保存在字符数组_cId中
        int[] a = new int[_cId.length];
        int[] b = new int[_cId.length];
        for (int i = 0; i < _cId.length; i++){
            a[i] = Integer.parseInt(_cId[i]);
            HitCourse hc = dao.selectById(uId, a[i]);
            if(hc != null){
                return hc;
            }
        }

        for (int i = 0; i < _cId.length; i++){
            b[i] = HitCourse(uId, a[i]);
            if(b[i] == 0){      //返回uID为0,cId为课程id的对象，表示到该id处数据库操作异常
                HitCourse hc = new HitCourse();
                hc.setuId(0);
                hc.setcId(a[i]);
                return hc;
            }
        }
        return null;
    }

    public int ExitCourse(int uId, String cIds){
        String[] _cId = cIds.split(",");        //以”,“为分割符，切割传入字符并保存在字符数组_cId中
        int[] b = new int[_cId.length];
        for (int i = 0; i < _cId.length; i++){
            b[i] = ExitCourse(uId, Integer.parseInt(_cId[i]));
            if(b[i] == 0){
                return 0;
            }
        }
        return 1;
    }

}
